$(document).ready(function() {
	menuSelect('visualization');
	$('[name=visualizationType]').change(update);
	$('[name=aviRecord]').change(update);
	$('[name=zoomMode]').change(update);
	$('[name=submit]').click(onSubmitClick);
	update();
});

function update() {
	var type = $('[name=visualizationType] option:selected').val();
	var aviRecord = $('[name=aviRecord]')[0].checked;
	var zoomMode = $('[name=zoomMode] option:selected').val();
	
	if (type == 'topology') {
		$('[name=fadeButKeep]').removeAttr('disabled');
		$('[name=maxCircleRadius]').attr('disabled', true);
	} else if (type == 'heatmap') {
		$('[name=fadeButKeep]').attr('disabled', true);
		$('[name=maxCircleRadius]').removeAttr('disabled');
	}
	
	if (aviRecord) {
		$('[name=fps]').removeAttr('disabled');
	} else {
		$('[name=fps]').attr('disabled', true);
	}
	
	if (zoomMode == 'auto') {
		$('[name=minLatitude]').attr('disabled', true);
		$('[name=maxLatitude]').attr('disabled', true);
		$('[name=minLongitude]').attr('disabled', true);
		$('[name=maxLongitude]').attr('disabled', true);
	} else if (zoomMode == 'manual') {
		$('[name=minLatitude]').removeAttr('disabled');
		$('[name=maxLatitude]').removeAttr('disabled');
		$('[name=minLongitude]').removeAttr('disabled');
		$('[name=maxLongitude]').removeAttr('disabled');
	}
}

function onSubmitClick() {
	var names = ['minLatitude', 'maxLatitude', 'minLongitude', 'maxLongitude'];
	var displayNames = ['Min Latitude', 'Max Latitude', 'Min Longitude', 'Max Longitude'];
	var maxAbsValue = [90, 90, 180, 180];
	var values = [0, 0, 0, 0];
	var i;
	
	for (i = 0; i < names.length; i++) {
		var value = parseInt($('[name=' + names[i]+ ']').val());
		values[i] = value;
		if (isNaN(value)) {
			alert(displayNames[i] + ' must be a number');
			return false;
		} else if (Math.abs(value) > maxAbsValue[i]) {
			alert(displayNames[i] + ' must be between -' + maxAbsValue[i] + ' and ' + maxAbsValue[i]);
			return false;
		}
	}
	
	for (i = 0; i < names.length - 1; i+=2) {
		if (values[i] >= values[i+1]) {
			alert(displayNames[i] + ' must be smaller than ' + displayNames[i+1]);
			return false;
		}
	}

	$('[name=fadeButKeep]').removeAttr('disabled');
	$('[name=maxCircleRadius]').removeAttr('disabled');
	$('[name=fps]').removeAttr('disabled');
	$('[name=minLatitude]').removeAttr('disabled');
	$('[name=maxLatitude]').removeAttr('disabled');
	$('[name=minLongitude]').removeAttr('disabled');
	$('[name=maxLongitude]').removeAttr('disabled');
	
	return true;
}